package com.ly.os;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Classname LRULinkedHashMap
 * @Description TODO
 * @Date 2020/12/6 15:34
 * @Author 冷心影翼
 */
public class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V> {

	private static int maxCapacity = 16;

	private final static float DEFAULT_LOAD_FACTOR= 0.75f;

	public LRULinkedHashMap(int maxCapacity, float loadFactor) {
		super(maxCapacity, loadFactor,true);
		LRULinkedHashMap.maxCapacity = maxCapacity;
	}

	public LRULinkedHashMap() {
		super(maxCapacity,DEFAULT_LOAD_FACTOR,true);
	}

	@Override
	protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
		return size()>maxCapacity;
	}

	@Override
	public int size() {
		return super.size();
	}

	public static void main(String[] args) {
		LRULinkedHashMap<String,String> lruLinkedHashMap = new LRULinkedHashMap<>(4,0.75f);
		lruLinkedHashMap.put("a","A");
		lruLinkedHashMap.put("b","B");
		lruLinkedHashMap.put("c","C");
		lruLinkedHashMap.put("d","D");
		lruLinkedHashMap.get("a");
		lruLinkedHashMap.get("b");
		lruLinkedHashMap.put("e","E");
		lruLinkedHashMap.forEach((k,v)->{
			System.out.println(k +" "+v);
		});
	}
}
